const x = [-1, -1, -1, 0]; // x偏移 
const y = [-1, 0, 1, 1]; // y偏移 
const base_score = [
	[5, 20],
	[10, 50],
	[41, 150],
	[500, 500]
];

const clacSite = (role, chess) => {
  let a = clacScore(1, chess)
  let b = clacScore(2, chess)
  if (a[0] > b[0]) {
    console.log(a)
    return [a[1], a[2]];
  }
  console.log(b)
  return [b[1], b[2]];
}

const clacScore = (role, chess) => {
	let res = [];
	// 循环每一个落子
	for (let i = 0; i < chess.length; ++i) {
		for (let j = 0; j < chess[0].length; ++j) {
			// 已经有落子，跳过
			if (chess[i][j] != 0) continue;
			//分四个方向寻找相同的落子构造决策分数
			let score = 0;
			for (let m = 0; m < x.length; ++m) {
				let flag = 0;
				let empty = 0;
				// 向一个方向找4次 
				for (let k = 1; k < 5; ++k) {
					let r = i + x[m] * k;
					let c = j + y[m] * k;
					if (r < 0 || r >= chess.length || c < 0 || c >= chess[0].length) {
						break;
					}
					if (chess[r][c] == role) {
						flag++;
					} else if (chess[r][c] == 0) {
						empty++;
						break;
					} else {
						break;
					}
				}
				// 向反方向找4次 
				for (let k = 1; k < 5; ++k) {
					let r = i - x[m] * k;
					let c = j - y[m] * k;
					if (r < 0 || r >= chess.length || c < 0 || c >= chess[0].length) {
						break;
					}
					if (chess[r][c] == role) {
						flag++;
					} else if (chess[r][c] == 0) {
						empty++;
						break;
					} else {
						break;
					}
				}
				if (flag > 0 && empty > 0) {
					score += base_score[flag - 1 > 3 ? 3 : flag - 1][empty - 1];
				} else if (empty == 0 && flag > 0) {
					score += base_score[flag - 1 > 3 ? 3 : flag - 1][0];
				}
			}
			if (res.length == 0) {
				res = [score, i ,j];
			} else if(res[0] <= score){
				res = [score, i ,j];
			}
		}
	}
  if (res + '' == '0,14,14') {
    res = [0, 7, 7]
  }
	return res;
}

export default clacSite